3 use MediaWiki\Widget\TitlesMultiselectWidget
;
6 * Implements a tag multiselect input field for titles.
8 * Besides the parameters recognized by HTMLTitleTextField, additional recognized
10 * default - (optional) Array of titles to use as preset data
11 * placeholder - (optional) Custom placeholder message for input
13 * The result is the array of titles
15 * This widget is a duplication of HTMLUsersMultiselectField, except for:
16 * - The configuration variable changed to 'titles' (from 'users')
17 * - OOUI modules were adjusted for the TitlesMultiselectWidget
18 * - The PHP version instantiates a MediaWiki\Widget\TitlesMultiselectWidget
20 * @note This widget is not likely to remain functional in non-OOUI forms.
22 class HTMLTitlesMultiselectField
extends HTMLTitleTextField
{
23 public function __construct( $params ) {
25 // This overrides the default from HTMLTitleTextField
29 parent
::__construct( $params );
32 public function loadDataFromRequest( $request ) {
33 $value = $request->getText( $this->mName
, $this->getDefault() );
35 $titlesArray = explode( "\n", $value );
37 $titlesArray = array_values( array_filter( $titlesArray, function ( $title ) {
38 return trim( $title ) !== '';
40 // This function is expected to return a string
41 return implode( "\n", $titlesArray );
44 public function validate( $value, $alldata ) {
45 if ( !$this->mParams
['exists'] ) {
49 if ( is_null( $value ) ) {
53 // $value is a string, because HTMLForm fields store their values as strings
54 $titlesArray = explode( "\n", $value );
56 if ( isset( $this->mParams
['max'] ) ) {
57 if ( count( $titlesArray ) > $this->mParams
['max'] ) {
58 return $this->msg( 'htmlform-int-toohigh', $this->mParams
['max'] );
62 foreach ( $titlesArray as $title ) {
63 $result = parent
::validate( $title, $alldata );
64 if ( $result !== true ) {
72 public function getInputHTML( $value ) {
73 $this->mParent
->getOutput()->enableOOUI();
74 return $this->getInputOOUI( $value );
77 public function getInputOOUI( $value ) {
80 'name' => $this->mName
,
84 if ( isset( $this->mParams
['disabled'] ) ) {
85 $params['disabled'] = $this->mParams
['disabled'];
88 if ( isset( $this->mParams
['default'] ) ) {
89 $params['default'] = $this->mParams
['default'];
92 if ( isset( $this->mParams
['placeholder'] ) ) {
93 $params['placeholder'] = $this->mParams
['placeholder'];
95 $params['placeholder'] = $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
98 if ( isset( $this->mParams
['max'] ) ) {
99 $params['tagLimit'] = $this->mParams
['max'];
102 if ( isset( $this->mParams
['showMissing'] ) ) {
103 $params['showMissing'] = $this->mParams
['showMissing'];
106 if ( isset( $this->mParams
['input'] ) ) {
107 $params['input'] = $this->mParams
['input'];
110 if ( !is_null( $value ) ) {
111 // $value is a string, but the widget expects an array
112 $params['default'] = $value === '' ?
[] : explode( "\n", $value );
115 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
116 $params['infusable'] = true;
117 $params['classes'] = [ 'mw-htmlform-field-autoinfuse' ];
118 $widget = new TitlesMultiselectWidget( $params );
119 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
124 protected function shouldInfuseOOUI() {
128 protected function getOOUIModules() {
129 return [ 'mediawiki.widgets.TitlesMultiselectWidget' ];